package bawei.com.bilibili;

import android.content.Intent;

import android.content.res.AssetManager;

import android.graphics.Color;

import android.os.Bundle;


import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;



import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;


import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


import java.util.List;



import bawei.com.adapter.GameCentreAdapter;
import bawei.com.model.bean.GameCenterBean;

/**
 * 类描述:
 * 作者：cwm
 * 时间:2017/3/21 12:43
 * 邮箱:18310832074@163.com
 */

public class GameCentreActivity extends FragmentActivity {

    private Toolbar mToolbar;
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_center);
        mToolbar= (Toolbar) findViewById(R.id.toolbar);
        recyclerView= (RecyclerView) findViewById(R.id.recycle);
        recyclerView.setLayoutManager(new LinearLayoutManager(GameCentreActivity.this));
        mToolbar.setLogo(R.drawable.action_button_back_pressed_light);
        mToolbar.setTitle("游戏中心");
        mToolbar.setTitleTextColor(Color.WHITE);
        getdate();

    }

    public void getdate(){


        String s = readAssetsJson();
        GameCenterBean gameCenterBean = new Gson().fromJson(s, GameCenterBean.class);
        final List<GameCenterBean.ItemsBean> items = gameCenterBean.getItems();
        GameCentreAdapter adapter=new GameCentreAdapter(GameCentreActivity.this,items);
        recyclerView.setAdapter(adapter);
        adapter.setOnClikLiter(new GameCentreAdapter.OnClikLiter() {
            @Override
            public void recycOnClikLiter(int p) {
                Intent intent=new Intent(GameCentreActivity.this,WebViewActivity.class);
                intent.putExtra("title",items.get(p).getTitle());
                intent.putExtra("web",items.get(p).getDownload_link());
                startActivity(intent);
            }
        });
    }

    private String readAssetsJson() {

        AssetManager assetManager = getAssets();
        try {
            InputStream is = assetManager.open("gamecenter.json");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder stringBuilder = new StringBuilder();
            String str;
            while ((str = br.readLine()) != null) {
                stringBuilder.append(str);
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
